import Toast from 'tdesign-miniprogram/toast/index';
import request from '../../../utils/requestAdmin';
var md5 = require('../../../utils/md5.min.js');


Page({
  externalClasses: ['theme-wrapper-class'],
  data: {
    username: '',
    password: ''
  },
  privateData: {
    verifyTips: '',
  },
  onLoad(options) {

  },

  hasSava: false,

  onInputValue(e) {
    const { item } = e.currentTarget.dataset;
    const { value = '' } = e.detail;
    this.setData(
      {
        [`${item}`]: value,
      },
      () => {
        const { isLegal, tips } = this.onVerifyInputLegal();
        this.setData({
          submitActive: isLegal,
        });
        this.privateData.verifyTips = tips;
      },
    );
  },
  onPickArea() {
    this.setData({ areaPickerVisible: true });
  },


  onVerifyInputLegal() {
    const { username, password } = this.data;

    if (!username || !username.trim()) {
      return {
        isLegal: false,
        tips: '请填写账号',
      };
    }
    // if (!nameRegExp.test(name)) {
    //   return {
    //     isLegal: false,
    //     tips: '姓名仅支持输入中文、英文（区分大小写）、数字',
    //   };
    // }
    if (!password || !password.trim()) {
      return {
        isLegal: false,
        tips: '请填写密码',
      };
    }
    return {
      isLegal: true,
      tips: '提交成功',
    };
  },

  formSubmit() {
    var that = this;
    const { submitActive } = this.data;
    const { isLegal, tips } = this.onVerifyInputLegal();
    if (!isLegal) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: tips,
        icon: '',
        duration: 1000,
      });
      return;
    }
    if (!submitActive) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: this.privateData.verifyTips,
        icon: '',
        duration: 1000,
      });
      return;
    }
    var url = "/car/admin/upms/loginByUserAndPassword"
    var data = {
      username: this.data.username,
      password: md5(this.data.password),
      uuid: 1,
      captCode: '123456'
    }
    request.post(url, data, function (res) {
      if (res.code == 0) {
        console.log(res)
        that.hasSava = true;
        var admin = {
          partnerCode: res.data.partnerCode,
          token: res.data.token,
          user: res.data.user
        }
        getApp().globalData.admin = admin
        wx.setStorageSync('admin_token', res.data.token);
        wx.setStorageSync('admin_userInfo', JSON.stringify(getApp().globalData.admin));

        wx.redirectTo({
          url: '/pages/admin/usercenter/index',
        })
      }
    })



  }
});
